<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8' />
		<title>Batoto Checker</title>
		<script src="jquery-1.4.3.min.js"></script>
		<script type="text/javascript" >
		
			//listener when extension is enabled the first time (at browser launch)
			safari.application.addEventListener("validate", extensionLaunched, false);
			
			// listener when settings change
			safari.extension.settings.addEventListener("change", settingsChanged, false);
			
			// click on icon
			safari.application.addEventListener("command", commandClick, false);
			
			// timer
			var updater = safari.extension.globalPage.contentWindow.setInterval( backgroundPageLoad, safari.extension.settings.getItem("updates") * 1000 );
				
			
			var batotoURL = "http://www.batoto.net/";
			var divContainer = null;
			
			function backgroundPageLoad()
			{
				$.ajax({
					url: batotoURL,
					success: checkNewManga
				});
			}
			
			function checkNewManga(data)
			{
				initDiv();
						
				divContainer.innerHTML = data;
				var follows = document.getElementById("tab_content_forums").children[0];
				
				var relevantData = new Array();
				
				for (var i = 0; i < follows.children.length; ++i)
				{
					var liObject = follows.children[i];
					
					var title = liObject.children[0].text;
					var language = liObject.children[2].title;
					var chapter = liObject.children[3].innerText;
					var time = liObject.children[4].innerText;
					
					if (checkLanguage(language))
					{
						var dataStruct = { mangaTitle: title, lastChapter: chapter, lang: language, upTime: time };
						relevantData.push(dataStruct);
					}
				}
				
				if (relevantData.length > 0)
				{
					var tooltip = "Batoto new mangas";
					
					for (var i = 0; i < relevantData.length; ++i)
					{
						tooltip += "\n" + relevantData[i].mangaTitle + " (" + relevantData[i].lastChapter + ")";
					}
					
					$.each(safari.extension.toolbarItems, function(i, v) // this will only change the one the program is allowed to
						{
							v.badge = relevantData.length;
							v.toolTip = tooltip;
						}
					);
				}
				
				divContainer.innerHTML = "";
			}
			
			function initDiv()
			{
				if (divContainer == null)
				{
					divContainer = document.createElement('div');
					document.body.insertBefore(divContainer, null);
				}
			}
			
			function checkLanguage(language)
			{
				if (language == "English")
				{
					return safari.extension.settings.englishCheck;
				}
				else if (language == "French")
				{
					return safari.extension.settings.frenchCheck;
				}
				else if (language == "Spanish")
				{
					return safari.extension.settings.spanishCheck;
				}
				else if (language == "Portuguese")
				{
					return safari.extension.settings.portugueseCheck;
				}
				else if (language == "Italian")
				{
					return safari.extension.settings.italianCheck;
				}
				else if (language == "Russian")
				{
					return safari.extension.settings.russianCheck;
				}
				else if (language == "Arabic")
				{
					return safari.extension.settings.arabicCheck;
				}
				else if (language == "German")
				{
					return safari.extension.settings.germanCheck;
				}
				else if (language == "Turkish")
				{
					return safari.extension.settings.turkishCheck;
				}
				else
				{
					return false;
				}
			}
			
			function settingsChanged(event)
			{
				if (event.key == "updates")
				{
					safari.extension.globalPage.contentWindow.clearInterval(updater);
					updater = safari.extension.globalPage.contentWindow.setInterval( backgroundPageLoad, event.newValue * 1000);
				}
			}
			
			function extensionLaunched(event)
			{
				safari.application.removeEventListener("validate", extensionLaunched, false);
				backgroundPageLoad();
			}
			
			function commandClick(event)
			{
				if (event.command == "batotoNewTab" )
				{
					// check if Batoto is open somewhere
					for (i in safari.application.browserWindows)
					{
						for (j in safari.application.browserWindows[i].tabs)
						{
							if (safari.application.browserWindows[i].tabs[j].url && safari.application.browserWindows[i].tabs[j].url.indexOf(batotoURL) == 0)
							{
								var browserWindow = safari.application.browserWindows[i];
	
								browserWindow.activate();
								browserWindow.tabs[j].activate();
								return;
							}
						}
					}
	
					var newTab = safari.application.activeBrowserWindow.openTab();
					newTab.url = batotoURL;
				}
			}
			
		</script>
	</head>
</html>